package com.whereareyou.service.impl;

import com.google.inject.Provider;
import com.whereareyou.model.Relationship;
import com.whereareyou.model.User;
import com.whereareyou.service.RelationshipService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

/**
 * Created by 家玮 on 2014/7/27.
 */
public class RelationshipServiceImpl implements RelationshipService {
    private final EntityManager entityManager;
    final static Logger log = LoggerFactory.getLogger(RelationshipServiceImpl.class);

    @Inject
    public RelationshipServiceImpl(Provider<EntityManager> entityManagerProvider) {
        this.entityManager = entityManagerProvider.get();
    }

    @Override
    public List<Relationship> findAllByUserId(Serializable userId) {
        String queryString = "SELECT r FROM Relationship r where r.user1 = :userId or r.user2 = :userId";
        Query query = entityManager.createQuery(queryString);

        query.setParameter("userId", userId);
        log.debug(query.getResultList().toString());
        List<Relationship> result = query.getResultList();
        return result;
    }
}
